const mongoose = require("mongoose")


module.exports.login = async user => {
    try {
        const movieModel = mongoose.model("users")
        console.log('用户名', user)
        const result = await movieModel.find(user)
        await new Promise((resolve) => {
            setTimeout(() => {
                resolve("")
            }, 2000)
        })
        return result;
    } catch (e) {
        console.log('=============== login 异常: =====================');
        console.log(e);
        console.log('====================================');
    }
}
module.exports.collectionAdd = async user =>{

}
module.exports.register = async  user => {
    try {
        user.data = new Date()
        const usersModel = mongoose.model("users")
        user.userType = ['普通']
        user.check = false
        const result = await usersModel.create(user)
        return result;
    } catch (e) {
        console.log('=============== login 异常: =====================');
        console.log(e);
        console.log('====================================');
    }
}

//申请租车审核
module.exports.applyRent = async  id => {
    try {
        const usersModel = mongoose.model("users")
        const result = await usersModel.updateOne({_id:id},{check:true})
        return result;
    } catch (e) {
        console.log('=============== login 异常: =====================');
        console.log(e);
        console.log('====================================');
    }
}

module.exports.changeUser = async (user) =>{

    const usersModel = mongoose.model("users")
    const result = await usersModel.updateOne({_id:user.id},{carCard:user.carCard,
                                                             idCard:user.idCard,
                                                             place:user.place,
                                                             name:user.name,
                                                                })
    return result
}
module.exports.list = async  () => {
    try {
        const usersModel = mongoose.model("users")
        const result = await usersModel.find()
        return result;
    } catch (e) {
        console.log('=============== login 异常: =====================');
        console.log(e);
        console.log('====================================');
    }
}
module.exports.user = async (id) => {
    const usersModel = mongoose.model("users")
    const result = await usersModel.find({_id:id})
    return result;
}
module.exports.banUser = async  (data) => {
    try {
        const usersModel = mongoose.model("users")
        console.log(data)
        const result = await usersModel.updateOne({"_id":data.id},{$set:{ban:data.ban}})
        return result;
    } catch (e) {
        console.log('=============== login 异常: =====================');
        console.log(e);
        console.log('====================================');
    }
}
//申请租车审核通过
module.exports.applyRentPass = async data => {
    try {

        const usersModel = mongoose.model("users")
        let result=''
        if(data.pass){
             result = await usersModel.updateOne({_id:data.id},{check:false,$push:{userType:'租车'}})
        }else {
             result=  await usersModel.updateOne({_id:data.id},{check:false})
        }

        return result;
    } catch (e) {
        console.log('=============== login 异常: =====================');
        console.log(e);
        console.log('====================================');
    }
}
